package pe.gob.mtc.dgac.licencias.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import pe.gov.mtc.dgac.licencias.bean.HallazgoTO;
import pe.gov.mtc.dgac.licencias.bean.SacpTO;
import pe.gov.mtc.dgac.licencias.service.HallazgoService;
import pe.gov.mtc.dgac.licencias.service.SacpService;
import pe.gov.mtc.dgac.licencias.util.Utiles;

/**
 *
 * @author elvis
 */
@Controller
@Scope("session")
public class SacpController {

    @Autowired
    private SacpService sacpService;
    @Autowired
    private HallazgoService hallazgoService;
    
    @RequestMapping("/calidad/lsacp.htm")
    public String lsacp(@RequestParam("codigo") int id,HttpServletRequest request, ModelMap model) {
        System.out.println("calidad/lsacp");
        SacpTO sacp = new SacpTO();
        sacp.setCodHallazgo(  hallazgoService.obtenerHallazgo(id) );
        model.put("lsacp",sacpService.listarSaco( sacp ));
        model.put("bsacp", sacp);
        return "calidad/lsacp";
    }
    
    @RequestMapping("/calidad/bsacp.htm")
    public String bsacp(@Valid SacpTO bsacp, BindingResult result,HttpServletRequest request,ModelMap model) {
        System.out.println("calidad/bsacp");
        bsacp.setCodHallazgo(  hallazgoService.obtenerHallazgo( bsacp.getCodHallazgo().getCodHallazgo()) );
        model.put("lsacp",sacpService.listarSaco( bsacp ));
        model.put("bsacp", bsacp);
        return "calidad/bsacp";
    }
    
    @RequestMapping("/calidad/nsacp.htm")
    public String nsacp(@Valid SacpTO bsacp, BindingResult result,HttpServletRequest request, ModelMap model) {
        SacpTO sacp = new SacpTO();
        System.out.println("id hall "+ bsacp.getCodHallazgo().getCodHallazgo() );
        sacp.setCodHallazgo(bsacp.getCodHallazgo());
        model.put("nsacp", sacp );
        System.out.println("calidad/nsacp");
        return "calidad/nsacp";
    }
    
    @RequestMapping("/calidad/editSacp.htm")
    public String editSacp(@RequestParam("codigo") int id,HttpServletRequest request, ModelMap model) {
        System.out.println("calidad/msacp");
        model.put("nsacp", sacpService.obtenerSacp(id));
        return "calidad/nsacp";
    }
    
     @RequestMapping("/calidad/verSacp.htm")
    public String verSacp(@RequestParam("codigo") int id,HttpServletRequest request, ModelMap model) {
        System.out.println("calidad/msacp");
        model.put("dsacp", sacpService.obtenerSacp(id));
        return "calidad/dsacp";
    }
     
    @RequestMapping("/calidad/delSacp.htm")
    public String delSacp(@RequestParam("hall") int idHall,@RequestParam("codigo") int id,HttpServletRequest request, ModelMap model) {
        System.out.println("calidad/lsacp");
        sacpService.eliminarSacp(id);
        return lsacp(idHall, request, model);
    }
     
    @RequestMapping(value="/calidad/rsacp.htm",method=RequestMethod.POST)
    public String rsacp(@Valid SacpTO nsacp, BindingResult result,HttpServletRequest request, ModelMap model) {
        try {
            System.out.println("calidad/rsacp");
            nsacp.setFecAccionInmediata( Utiles.stringToDate( request.getParameter("fecAccionInmediata") , "dd/MM/yyyy"));
            nsacp.setFecPrimerSeg( Utiles.stringToDate( request.getParameter("fecPrimerSeg") , "dd/MM/yyyy"));
            nsacp.setFecSegundoSeg( Utiles.stringToDate( request.getParameter("fecSegundoSeg") , "dd/MM/yyyy"));
            sacpService.registrarSacp(nsacp);
            return lsacp(nsacp.getCodHallazgo().getCodHallazgo().intValue(), request, model);    
        } catch (Exception e) {
            e.printStackTrace();
            nsacp.setCodSacp(null);
            model.put("nsacp", nsacp);
            model.put("msgError", e.getMessage());
            return "calidad/nsacp";
        }
    }
   
    
}
